export default function ({
  route,
  redirect,
  req
}) {
  const originalPath = route.path;
  const lowercasePath = originalPath.toLowerCase();

  let isMobile = ua => {
    return !!ua.match(/AppleWebKit.*Mobile.*/)
  }
  let userAgent = req ? req.headers['user-agent'] : navigator.userAgent || ''
  if (!req) return

  if (!isMobile(userAgent)) {} else {
    redirect(301, 'https://m.xzcs2022.com' + lowercasePath)
    // console.log('移动端')
    return;
  }


  if (lowercasePath.includes('/information/') && lowercasePath.includes('/detail/')) {
    let newPaths = lowercasePath.split('/')
    let newPath = newPaths[newPaths.length - 1]
    redirect(301, '/information/' + newPath);
  } else if (lowercasePath.includes('/quizdata/') && lowercasePath.includes('/detail/')) {
    let newPaths = lowercasePath.split('/')
    let newPath = newPaths[newPaths.length - 1]
    redirect(301, '/quizdata/' + newPath);
  } else if ((lowercasePath.includes('/ssfgk') || lowercasePath.includes('/kjfgk')) && lowercasePath.includes('details/')) {
    let newPaths = lowercasePath.split('/')
    let newPath = newPaths[newPaths.length - 1]
    if (lowercasePath.includes('/ssfgk')) {
      redirect(301, '/ssfgk/' + newPath);
    }
    if (lowercasePath.includes('/kjfgk')) {
      redirect(301, '/kjfgk/' + newPath);
    }
  } else if (lowercasePath.includes('/modeldetail/')) {
    let newPath = lowercasePath.replace('/modeldetail/', "")
    redirect(301, '/templates/' + newPath);
  } else if (lowercasePath.includes('/secretbookdetail/')) {
    let newPath = lowercasePath.replace('/secretbookdetail/', "")
    redirect(301, '/cswd/' + newPath);
  } else if (lowercasePath.includes('/informationdetails')) {
    redirect(301, '/404');
  } else if (originalPath !== lowercasePath) {
    redirect(301, lowercasePath);
  }
}
